package com.gugee.ins.web.service;

import com.gugee.common.web.util.ESUtil;
import com.gugee.ins.web.dto.InsBloggerSearchData;
import com.gugee.ins.web.model.es.InsBlogger;
import com.gugee.ins.web.model.mysql.InsDicGeneration;
import com.xiguaji.boot.toolkit.elasticsearch.RestClientHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;
import lombok.val;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class InsBloggerService {

    int [] genders = new int[]{3,2};

    int [] followers = new int[]{1,5,10,50,100,500,1000};
    int [] ers = new int[]{1,3,5,10,20,30};
    int [] followerGrowths = new int[]{5,10,20,30,50};
    String [] audienceGenders = new String[]{"MALE","FEMALE"};

    List<InsDicGeneration> ages;

    @Autowired
    DicService dicService;

    @PostConstruct
    public void init(){
        ages = dicService.getDicGenerationByType(1);
    }


    public Page<InsBlogger> getPageByEs(InsBloggerSearchData searchData){
        /*SearchRequest searchRequest = new SearchRequest(ESUtil.INS_BLOGGER_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();*/
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        if(!StringUtils.isEmpty(searchData.getUsername())){
            boolQueryBuilder.must(QueryBuilders.wildcardQuery("username","*"+searchData.getUsername()+"*"));
        }

        // 提及的用户
        if(!StringUtils.isEmpty(searchData.getTaggedUsername())){
            boolQueryBuilder.must(QueryBuilders.nestedQuery("taggedBrand",QueryBuilders.matchQuery("taggedBrand.username",searchData.getTaggedUsername()), ScoreMode.Total));
        }

        //国家
        if(!StringUtils.isEmpty(searchData.getRegion())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("region",searchData.getRegion()));
        }
        //语言
        if(!StringUtils.isEmpty(searchData.getRegion())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("language",searchData.getRegion()));
        }
        //性别
        if(searchData.getGender() > 0){
            int index = searchData.getGender()-1;

            if(index >= genders.length){
                boolQueryBuilder.must(QueryBuilders.matchQuery("gender",genders[genders.length-1]));
            }
            boolQueryBuilder.must(QueryBuilders.matchQuery("gender",genders[index]));
        }
        //年龄
        if(searchData.getAge() > 0){
            int index = searchData.getAge()-1;

            val insDicGeneration = ages.get(index);

            boolQueryBuilder.must(QueryBuilders.rangeQuery("age").gte(insDicGeneration.getGenerationStart()).lte(insDicGeneration.getGenerationEnd()));
        }
        //认证方式
        if(searchData.getVerification() > 0){
            if(searchData.getVerification() > 1){
                boolQueryBuilder.must(QueryBuilders.matchQuery("isVerified",false));
            }else {
                boolQueryBuilder.must(QueryBuilders.matchQuery("isVerified",true));
            }
        }
        //联系信息
        if(searchData.getContact() > 0){
            BoolQueryBuilder boolQueryBuilderContact = new BoolQueryBuilder();
            boolQueryBuilderContact.mustNot(QueryBuilders.termQuery("contactPhoneNumber",""));
            boolQueryBuilderContact.mustNot(QueryBuilders.termQuery("publicEmail",""));
            boolQueryBuilder.should(boolQueryBuilderContact);
        }
        //粉丝数
        if(searchData.getFollowers() > 0){
            int index = searchData.getFollowers() -1;
            if(index >= followers.length-1){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("followerCount").gte(followers[followers.length-1]*10000));
            }else{
                boolQueryBuilder.must(QueryBuilders.rangeQuery("followerCount").gte(followers[index]*10000).lte(followers[index+1]*10000));
            }
        }
        //粉丝数自定义
        if(null != searchData.getFollowersDiy() && searchData.getFollowersDiy().getTo() > 0){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("followerCount").gte(searchData.getFollowersDiy().getFrom()).lte(searchData.getFollowersDiy().getTo()));
        }
        //真实粉丝数
        if(searchData.getQualityFollowers() > 0){
            int index = searchData.getQualityFollowers() -1;
            if(index >= followers.length-1){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("realFollowerCount").gte(followers[followers.length-1]*10000));
            }else{
                boolQueryBuilder.must(QueryBuilders.rangeQuery("realFollowerCount").gte(followers[index]*10000).lte(followers[index+1]*10000));
            }
        }
        //真实粉丝数自定义
        if(null != searchData.getQualityFollowersDiy() && searchData.getQualityFollowersDiy().getTo() > 0){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("realFollowerCount").gte(searchData.getQualityFollowersDiy().getFrom()).lte(searchData.getQualityFollowersDiy().getTo()));
        }
        //参与率
        if(searchData.getEr() > 0){
            int index = searchData.getEr() -1;
            if(index >= ers.length-1){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("engagementRate").gte(ers[ers.length-1]/100));
            }else{
                boolQueryBuilder.must(QueryBuilders.rangeQuery("engagementRate").gte(ers[index]/100).lte(ers[index+1]/100));
            }
        }
        //参与率自定义
        if(null != searchData.getErDiy() && searchData.getErDiy().getTo() > 0){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("engagementRate").gte(searchData.getErDiy().getFrom()/100).lte(searchData.getErDiy().getTo()/100));
        }
        //粉丝增长率
        if(searchData.getFollowerGrowth() > 0){
            int index = searchData.getFollowerGrowth() -1;
            if(index == 0){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("followerGrowRate").lt(followerGrowths[index]/100));
            }else if(index >= followerGrowths.length){
                boolQueryBuilder.must(QueryBuilders.rangeQuery("followerGrowRate").gte(followerGrowths[followerGrowths.length-1]/100));
            }else{
                boolQueryBuilder.must(QueryBuilders.rangeQuery("followerGrowRate").gte(followerGrowths[index-1]/100).lte(followerGrowths[index]/100));
            }
        }
        //粉丝增长率自定义
        if(null != searchData.getFollowerGrowthDiy() && searchData.getFollowerGrowthDiy().getTo() > 0) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("followerGrowthRate").gte(searchData.getFollowerGrowthDiy().getFrom()/100).lte(searchData.getFollowerGrowthDiy().getTo()/100));
        }
        //粉丝最多的国家
        if(!StringUtils.isEmpty(searchData.getAudienceRegionMore())){
            BoolQueryBuilder regionQuery = new BoolQueryBuilder();
            regionQuery.must(QueryBuilders.matchQuery("nationDistribution.property",searchData.getAudienceRegionMore()));
            regionQuery.must(QueryBuilders.matchQuery("nationDistribution.rank",1));
            boolQueryBuilder.must(QueryBuilders.nestedQuery("nationDistribution",regionQuery, ScoreMode.Total));
        }
        //粉丝最多的语言
        if(!StringUtils.isEmpty(searchData.getAudienceLanguageMore())){
            BoolQueryBuilder languageQuery = new BoolQueryBuilder();
            languageQuery.must(QueryBuilders.matchQuery("languageDistribution.property",searchData.getAudienceLanguageMore()));
            languageQuery.must(QueryBuilders.matchQuery("languageDistribution.rank",1));
            boolQueryBuilder.must(QueryBuilders.nestedQuery("languageDistribution",languageQuery, ScoreMode.Total));
        }
        //粉丝最多的性别
        if(searchData.getAudienceGenderMore() > 0 && searchData.getAudienceGenderPercent() == 0){
            BoolQueryBuilder genderQuery = new BoolQueryBuilder();
            genderQuery.must(QueryBuilders.matchQuery("genderDistribution.property",audienceGenders[searchData.getAudienceGenderMore()-1]));
            genderQuery.must(QueryBuilders.matchQuery("genderDistribution.rank",1));
            boolQueryBuilder.must(QueryBuilders.nestedQuery("genderDistribution",genderQuery, ScoreMode.Total));
        }
        //粉丝最多的年龄
        if(searchData.getAudienceAgeMore()> 0&& searchData.getAudienceAgePercent() == 0){
            BoolQueryBuilder ageQuery = new BoolQueryBuilder();
            ageQuery.must(QueryBuilders.matchQuery("ageDistribution.property",ages.get(searchData.getAudienceAgeMore()-1).getGenerationName()));
            ageQuery.must(QueryBuilders.matchQuery("ageDistribution.rank",1));
            boolQueryBuilder.must(QueryBuilders.nestedQuery("ageDistribution",ageQuery, ScoreMode.Total));
        }

        //粉丝性别自定义查询
        if(searchData.getAudienceGenderMore() > 0 && searchData.getAudienceGenderPercent() > 0){
            BoolQueryBuilder genderQuery = new BoolQueryBuilder();
            genderQuery.must(QueryBuilders.matchQuery("genderDistribution.property",audienceGenders[searchData.getAudienceGenderMore()-1]));
            genderQuery.must(QueryBuilders.rangeQuery("genderDistribution.value").gt(searchData.getAudienceGenderPercent()/100));
            boolQueryBuilder.must(QueryBuilders.nestedQuery("genderDistribution",genderQuery, ScoreMode.Total));
        }

        if(searchData.getAudienceAgeMore() > 0 && searchData.getAudienceAgePercent() > 0){
            BoolQueryBuilder ageQuery = new BoolQueryBuilder();
            ageQuery.must(QueryBuilders.matchQuery("ageDistribution.property",ages.get(searchData.getAudienceAgeMore()-1).getGenerationName()));
            ageQuery.must(QueryBuilders.rangeQuery("ageDistribution.value").gt(searchData.getAudienceAgePercent()/100));
            boolQueryBuilder.must(QueryBuilders.nestedQuery("ageDistribution",ageQuery, ScoreMode.Total));
        }
        return RestClientHelper.page(InsBlogger.class, ESUtil.INS_BLOGGER_INDEX,boolQueryBuilder,searchData.getPage(),searchData.getPageSize(),"followerCount", SortOrder.DESC);
    }
}
